<?php
/**
 * 成绩详细页
 */
//session_start();
include "../util/DBUtil.php";
//$login_type = $_SESSION['login_type'];//登录类型
//$login_user = unserialize($_SESSION['login_user']);//反解序列获登录用户信息

$db = new DBUtil();

$sql = "select * from tb_course";
$course_result = $db->query($sql);//课程结果集

$sql = "select * from v_student";//获取课程表中已经有学生的信息
$student_result = $db->query($sql);
if ($login_type==2){//教师显示本班学生
    $sql = "select * from v_student where grade_id=? ";
    $params = array($login_user['grade_id']);
    $student_result = $db->query($sql,$params);
}elseif ($login_type==0){//学生登录显示学生成绩
    $sql = "select * from v_student where id=? ";
    $params = array($login_user['id']);
    $student_result = $db->query($sql,$params);
}

/*关键字查询*/
$key = $_GET['key'];
if ($key != "") {
    $key_type = $_GET['key_type'];
    if ($key_type == 0) {//学号查询
        $sql = "select * from v_student where student_no like ? ";
        $params = array('%'.$key.'%');
        if ($login_type==2){//教师显示本班学生
            $sql = "select * from v_student where grade_id=? and student_no like ? ";
            $params = array($login_user['grade_id'],'%'.$key.'%');
        }
    }elseif ($key_type==1){//名字查询
        $sql = "select * from v_student where name like ? ";
        $params = array('%'.$key.'%');
        if ($login_type==2){//教师显示本班学生
            $sql = "select * from v_student where grade_id=? and name like ? ";
            $params = array($login_user['grade_id'],'%'.$key.'%');
        }
    }
    $student_result = $db->query($sql,$params);
}
?>


<link rel="stylesheet" href="../css/bootstrap.min.css">
<link rel="stylesheet" href="../css/hidden.css">
<table class="table table-striped">
    <tr>
        <th>ID</th>
        <th>学号</th>
        <th>姓名</th>
        <?php
        foreach ($course_result as $item) {
            ?>
            <th><?php echo $item['course'] ?></th>
            <?php
        }
        ?>
        <th class="<?php echo $login_type!=2? 'hidden':''?>">编辑</th>
    </tr>
    <?php
    foreach ($student_result as $value) {
        ?>
        <tr>
            <td><?php echo $value['id'] ?></td>
            <td><?php echo $value['student_no'] ?></td>
            <td><?php echo $value['name'] ?></td>
            <!--显示分数-->
            <?php
            $sql = "select * from tb_score where student_id=?";
            $params = array($value['id']);
            $score_result = $db->query($sql, $params);
            foreach ($score_result as $score) {
                ?>
                <td><?php echo $score['score'] ?></td>
                <?
            }
            /*还没有登记分数的科目*/
            $score_len = count($score_result);
            $course_len = count($course_result);
            for ($i = 0, $len = $course_len - $score_len; $i < $len; $i++) {
                ?>
                <td>0</td>
                <?php
            }
                ?>
                <td class="<?php echo $login_type!=2? 'hidden':''?>">
                    <a href="../view/edit_score.php?id=<?php echo $value['id'] ?>&flag=0" target="info">
                        <button class="btn btn-primary">修改</button>
                    </a>
                    <a href="../view/edit_score.php?id=<?php echo $value['id'] ?>&flag=1" target="info">
                        <button onclick="alert('是否删除？')" class="btn btn-danger">删除</button>
                    </a>
                </td>

        </tr>
        <?
    }
    ?>
</table>
